/*
 * Copyright 2007-2010 Jeremy Brown.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author <a href="http://jeremy-brown.com">Jeremy Brown</a>
 */

package groovy.prefuse.demos

import java.awt.*
import javax.swing.*

import prefuse.*
import prefuse.action.*
import prefuse.action.assignment.*
import prefuse.action.layout.graph.*
import prefuse.activity.*
import prefuse.controls.*
import prefuse.data.*
import prefuse.render.*
import prefuse.util.*
import prefuse.util.ui.*
import prefuse.visual.*
import prefuse.visual.expression.*

import groovy.swing.*
import groovy.prefuse.*
import groovy.prefuse.factory.*

/**
 * A simple scatter plot visualization that allows visual encodings to
 * be changed at runtime. Modified to use Groovy, SwingBuilder, and PrefuseBuilder
 * @author <a href="http://jheer.org">jeffrey heer</a>
 * @author <a href="http://www.jeremy-brown.com">Jeremy Brown</a>
 */
public class ScatterPlot 
{
	public static PrefuseBuilder swing

	public static void main(String[] argv)
	{
		String data = "data/fisher.iris.txt";
		String xfield = "SepalLength";
		String yfield = "PetalLength";
		String sfield = "Species";
		if ( argv.length >= 3 )
		{
			data = argv[0];
			xfield = argv[1];
			yfield = argv[2];
			sfield = ( argv.length > 3 ? argv[3] : null );
		}
        
		
		//this will hold a list of data column names
		def colnames = []

		//Set up the Swing layout
		swing = new PrefuseBuilder()
		
		int spacing = 10

		def frame = swing.frame(title:'p r e f u s e | s c a t t e r', preferredSize:[750, 550], defaultCloseOperation:javax.swing.WindowConstants.EXIT_ON_CLOSE)
		{
			borderLayout()

			hbox()
			{
				//Create Prefuse table, visualization, and display. Display as a Scatter Plot.
				pfDisplay(id:"display", size:[350, 550], highQuality:true, border:BorderFactory.createEmptyBorder(10,10,10,10))
				{
					pfControl(ToolTipControl, fields:[xfield,yfield])
					pfVisualization(id:"vis", defaultGroup:"data")
					{
						pfTable(file:"$data", hasHeader:true, id:"table")

						pfShapeRenderer(baseSize:10, id:"shapeR")

						pfActionList(id:"draw", name:"draw")
						{
							pfAxisLayout(id:"x_axis", name:"x", dataField:xfield, axis:Constants.X_AXIS, filter:VisiblePredicate.TRUE)
							pfAxisLayout(id:"y_axis", name:"y", dataField:yfield, axis:Constants.Y_AXIS, filter:VisiblePredicate.TRUE)
							pfDataShapeAction(id:"shape", name:"shape", dataField:sfield)
							pfColorAction(id:"color", name:"color", field:VisualItem.STROKECOLOR, defaultColor:[100,100,255])
							pfRepaintAction()
						}
					}
				}
				
				scrollPane()
				{
					pfJPrefuseTable(table)
				}
			}

			//add list of data column names that exist in the table into the colnames variable
			(0..<swing.table.getColumnCount()).each{colnames << swing.table.getColumnName(it)}
			
			toolBar(constraints:BorderLayout.NORTH)
			{
				boxLayout()
				hstrut(width:spacing)
				label("X: ")
				comboBox(id:"xcb", items:colnames, selectedItem:xfield, 
					actionPerformed:
					{
						swing.x_axis.setDataField((String)swing.xcb.getSelectedItem())
						swing.vis.run("draw")
					}
				)
				hstrut(width:2*spacing)
				label("Y: ")
				comboBox(id:"ycb", items:colnames, selectedItem:yfield,
					actionPerformed:
					{
						swing.y_axis.setDataField((String)swing.ycb.getSelectedItem())
						swing.vis.run("draw")
					}
				)
				hstrut(width:2*spacing)
				label("Shape: ")
				comboBox(id:"scb", items:colnames, selectedItem:sfield,
					actionPerformed:
					{
						swing.shape.setDataField((String)swing.scb.getSelectedItem())
						swing.vis.run("draw")
					}
				)
				hstrut(spacing)
				hglue()
			}
		}
		
		frame.pack()
		frame.show()
		swing.vis.run("draw");
		
    }
} // end of class ScatterPlot